/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interpreter;

import ast.AST;
import java.util.Date;

/**
 *
 * @author Stephen
 */
public class LessThanOrEqualOperator {
    private AST t;
    private InterpreterVisitor iv;

    public LessThanOrEqualOperator(AST t, InterpreterVisitor iv) {
        this.t = t;
        this.iv = iv;
    }

    public Object execute() {
        Object operand1 = t.getKid(1).accept(iv);
        Object operand2 = t.getKid(2).accept(iv);
        try {
            if (operand1.getClass() == Class.forName("java.lang.String")) {
                String type = InterpreterVisitor.parseType((String) operand1);

                if (type.equalsIgnoreCase("float")) {
                    return (Float.parseFloat((String) operand1) <= (Float.parseFloat((String) operand2)));
                } else if (type.equalsIgnoreCase("int")) {
                    return (Integer.parseInt((String) operand1) <= (Integer.parseInt((String) operand2)));
                }
            } else if (operand1.getClass() == Class.forName("java.util.Date")) {
                return ((Date)operand1).before((Date)operand2) || ((Date)operand1).equals((Date)operand2);
            }
        } catch (ClassNotFoundException ex) {
            System.out.println(ex.toString());
        }

        throw new UnsupportedOperationException("Not supported yet.");
    }
}
